Seamless R and C++ Integration with Rcpp by Dirk Eddelbuettel
Author:Dirk Eddelbuettel
Language: eng
Format: epub
Publisher: Springer New York, New York, NY
7.4 Case Study: The RcppCNPy Package
Modules are a very powerful tool. They are suited to exposing code from existing class libraries as the RcppBDT package discussed in the previous section illustrates. The Rcpp attributes system uses modules to easily connect the wrappers it generates for the user-supplied code.
Modules can be used to provide simple wrappers to external libraries. A simple example is provided by the RcppCNPy package (Eddelbuettel 2012a). It uses a small stand-alone library provided in a single header and source files in order to access NumPy files used by this popular Python extension.
In the package, two functions npyLoad() and npySave() are defined. They simply transfer data between a given file name and R by relying on the external library provided by the source files cnpy.cpp and cnpy.h.
Listing 7.40 shows simplified versions of these two functions. We have omitted several aspects to keep the exposition shorter: the special case of transposing, the additional layer of transparently dealing with gzip-compressed files, the support for long long types, as well as the support for different underlying data types concentratic on just numeric.
Listing 7.40 NumPy load and save functions defined in RcppCNPy
Rcpp::RObject npyLoad(const std::string & filename,
2 const std::string & type) {
cnpy::NpyArray arr;
4 arr = cnpy::npy_load(filename);
6 std::vector<unsigned int> shape = arr.shape;
SEXP ret = R_NilValue;
8 if (shape.size() == 1) {
if (type == "numeric") {
10 double *p = reinterpret_cast<double*>(arr.data);
ret = Rcpp::NumericVector(p, p + shape[0]);
12 } else {
arr.destruct();
14 Rf_error("Unsupported type in npyLoad");
}
16 } else if (shape.size() == 2) {
if (type == "numeric") {
18 ret = Rcpp::NumericMatrix(shape[0], shape[1],
reinterpret_cast<double*>(arr.data));
20 } else {
arr.destruct();
22 Rf_error("Unsupported type in npyLoad");
}
24 } else {
arr.destruct();
26 Rf_error("Unsupported dimension in npyLoad");
}
28 arr.destruct();
return ret;
30 }
32 void npySave(std::string filename, Rcpp::RObject x,
std::string mode) {
34 if (::Rf_isMatrix(x)) {
if (::Rf_isNumeric(x)) {
36 Rcpp::NumericMatrix mat =
transpose(Rcpp::NumericMatrix(x));
38 std::vector<unsigned int> shape =
Rcpp::as<std::vector<unsigned int> >(
40 Rcpp::IntegerVector::create(mat.ncol(),
mat.nrow()));
42
cnpy::npy_save(filename, mat.begin(),
44 &(shape[0]), 2, mode);
} else {
46 Rf_error("Unsupported matrix type\n");
}
48 } else if (::Rf_isVector(x)) {
if (::Rf_isNumeric(x)) {
50 Rcpp::NumericVector vec(x);
std::vector<unsigned int> shape =
52 Rcpp::as<std::vector<unsigned int> >(
Rcpp::IntegerVector::create(vec.length()));
54 cnpy::npy_save(filename, vec.begin(),
&(shape[0]), 1, mode);
56 } else {
Rf_error("Unsupported vector type\n");
58 }
} else {
60 Rf_error("Unsupported type\n");
}
62 }
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(25279)
Hello! Python by Anthony Briggs(24320)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(23417)
Kotlin in Action by Dmitry Jemerov(22500)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(21952)
Dependency Injection in .NET by Mark Seemann(21834)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(20696)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(19513)
Grails in Action by Glen Smith Peter Ledbrook(18591)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17028)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(15836)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(13683)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(11844)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11149)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10619)
Hit Refresh by Satya Nadella(9185)
The Kubernetes Operator Framework Book by Michael Dame(8560)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8398)
Robo-Advisor with Python by Aki Ranin(8344)